import { SAVING_STATUS } from '@utility/dict'

let savingTimer = null
// let syncSensitiveTimer = null

export default {
  namespaced: true,
  state: () => ({
    savingStatus: SAVING_STATUS.init, //  0：初始化 1 保存中  2 保存成功  -1 保存失败
    editIndInfo: null,
  }),
  getters: {
    savingStatus: state => state.savingStatus,
    editIndInfo: state => state.editIndInfo,
  },
  actions: {},
  mutations: {
    setSavingStatus(state, status) {
      if (status === SAVING_STATUS.saving) {
        state.startSavingTime = new Date()
        if (savingTimer) {
          clearTimeout(savingTimer)
        }
        state.savingStatus = status
      } else {
        const savingGap = new Date() - state.startSavingTime
        if (state.startSavingTime && savingGap < 300) {
          savingTimer = setTimeout(() => {
            state.savingStatus = status
          }, 300 - savingGap)
        } else {
          state.savingStatus = status
        }
      }
    },
    setEditIndInfo(state, info) {
      state.editIndInfo = info
    },
  },
}
